package com.signway.utils;

import android.util.Log;

import java.io.IOException;

/**
 * 执行runtime命令工具类
 */
public class RuntimeToolsUtils {
    private static final String TAG = "RuntimeTools";

    public static Process execRootCommand(String command) throws IOException {
        if (command == null) {
            Log.w(TAG, "command is null");
            return null;
        }

//        Log.i(TAG, "execRootCommand: " + command);
        Runtime runtime = Runtime.getRuntime();
        Process proc = runtime.exec(command);
//		Runtime runtime = Runtime.getRuntime();
//		Process proc = runtime.exec("su");
//
//		OutputStream os = proc.getOutputStream();
//		String cmd = command + "\n";
//		os.write(cmd.getBytes());
//		os.flush();

        return proc;
    }

    public static Process execCommand(String command) throws IOException {
        if (command == null) {
            Log.w(TAG, "command is null");
            return null;
        } else {
//            Log.v(TAG, "execCommand: " + command);
        }

        Runtime runtime = Runtime.getRuntime();
        Process proc = runtime.exec(command);

        return proc;
    }

    public static boolean execProtectRootCommand(String command){
        try {
            execRootCommand(command);
            return true;
        } catch (IOException e1) {
            Log.e(TAG, "exec failed " + e1.toString());
        }

        return false;
    }

    public static boolean execProtectCommand(String command){
        try {
            execCommand(command);
            return true;
        } catch (IOException e1) {
            Log.e(TAG, "exec failed " + e1.toString());
        }

        return false;
    }

    public static Process execProcessCommand(String command){
        try {
            return execCommand(command);
        } catch (IOException e1) {
            Log.e(TAG, "exec failed " + e1.toString());
        }

        return null;
    }
}
